package com.gues.ddc_app.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if(handler instanceof HandlerMethod){
            Object user = request.getSession().getAttribute("user");
            Object shop = request.getSession().getAttribute("shop");
            Object sysAdmin = request.getSession().getAttribute("admin");
            if(sysAdmin!=null||user!=null||shop!=null){
                return true;
            }else {
                System.out.println(requestURI+"试图访问，拦截成功");
                request.getRequestDispatcher("/login").forward(request,response);
                return false;
            }
        }
        return true;
    }
}
